<template>
  <div class="topbar">
    <div class="wrap topbarcenter">
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" />
          用户名 : {{ userInfo.nickName }}
        </li>
        <li>我的积分: {{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>

        <li class="cart-btn" v-if="isLoginStatus">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="loginbutton" @click="changeIsShowLoginModal(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";

import { WeiXinLoginAPI } from "@/request/Api";
export default {
  computed: {
    ...mapState({
      isLoginStatus: (state) => state.loginStatus.isLoginStatus,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeLoginStatus: "loginStatus/changeLoginStatus",
      initUserInfo: "userInfo/initUserInfo",
    }),

    ...mapActions({
      asyncIsShowToast: "showToast/asyncIsShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  created() {
    // console.log("组件重新加载了。。topbar");

    // 组件重载会重新执行这里
    this.$router.onReady(async () => {
      let wxCode = this.$route.query.code;
      if (wxCode) {
        let res = await WeiXinLoginAPI({
          code: wxCode,
        });

        if (res.code == 0) {
          // 登录成功
          this.asyncIsShowToast({
            isShowToast: true,
            msg: "登录成功!",
            type: "success",
          });

          //2.存储token
          localStorage.setItem("x-auth-token", res["x-auth-token"]);

          //3.改变登录变为购物车
          this.changeLoginStatus(true);

          // 更新用户信息
          this.asyncChangeUserInfo();
        } else if (res.code == 400) {
          //1.提示 登录失败
          this.asyncIsShowToast({
            isShowToast: true,
            msg: "请重新扫码登录!",
            type: "danger",
          });

          // 2.引导用户
          this.changeIsShowLoginModal(true);
        } else if (res.code == 407) {
          //1. 提示第一次登录
          this.asyncIsShowToast({
            isShowToast: true,
            msg: "请使用手机号再登录一次!",
            type: "warning",
          });

          // 2.引导弹出登录框
          this.changeIsShowLoginModal(true);

          // 3.本地存储存入uuid，后续使用
          localStorage.setItem("uuid", res.uuid);

          // 4.把地址栏的code删除
          this.$router.push(this.$route.path);
        }
      } else {
        let token = localStorage.getItem("x-auth-token");
        this.changeLoginStatus(Boolean(token));
        if (token) {
          // 更新用户信息
          this.asyncChangeUserInfo();
        } else {
          // 用户信息初始化
          this.initUserInfo();
        }
      }
    });
  },

  // 实现：手动删除token，切换路由从而更新登录状态

  // 方法一
  // 监听路由地址
  // watch: {
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       let token = localStorage.getItem("x-auth-token");

  //       if (token) {
  //         this.changeLoginStatus(true);
  //       } else {
  //         this.changeLoginStatus(false);
  //       }
  // this.changeLoginStatus(Boolean(token));
  //     },
  //   },
  // },

  // 方法二： 看App.vue
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;

  .topbarcenter {
    display: flex;
    justify-content: space-between;

    .right {
      li {
        float: left;
        padding: 0 12px;
        text-align: center;
        cursor: pointer;
        img {
          width: 25px;
          height: 25px;
          border-radius: 13px;
          vertical-align: middle;
        }
      }
      .loginbutton {
        width: 110px;
        height: 40px;
        padding: 0 10px;
        background-color: #0a328e;
        cursor: pointer;
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>